const Koa = require('koa'),
    routers =  require('koa-router')(),
    static = require('koa-static'),
    path = require('path'),
    onerror = require('koa-onerror'),
    logger = require('koa-logger'),
    json = require('koa-json'),
    bodyParser = require('koa-bodyparser'),
    cors = require('koa2-cors');

const app = new Koa();
onerror(app)

app.use(static(__dirname+'/public'));
app.use(bodyParser());
app.use(cors());
app.use(json());
app.use(logger());


//配置计算和显示请求URL 请求方法耗时间 以及文件大小
app.use(function *(next){
    var start = new Date;
    yield next;
    var ms = new Date - start;
    console.log('&s &s - &s',this.method,this.url,ms)
});


//配置子路由
const index = require('./routes/index');
routers.use('/api/v2',index);
const users = require('./routes/xxx/user');
routers.use('/login',users);
// const dl = require('./routes/xxx/dl');
// routers.use('/login',dl);




app.on('error',(err,ctx)=>{
    console.log('Error-666:',err,ctx)
});
app.use(routers.routes());
app.use(routers.allowedMethods());
app.listen(10002);
